package com.lill.stock.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.lill.common.component.model.P;
import com.lill.common.component.util.PageUtils;
import com.lill.common.component.util.Query;
import com.lill.common.component.util.R;
import com.lill.stock.model.StockRouteModel;
import com.lill.stock.service.IStockRouteService;
import com.lill.support.annotation.CurrentUser;
import com.lill.system.model.SysUserEntity;

/**
 * 车辆在途Controller
 *
 * @author lill
 * @date 2021-09-09 11:20:55
 */
@RestController
@RequestMapping("stock/route")
public class StockRouteController {
	
	protected Logger logger = LoggerFactory.getLogger(getClass());
	
    @Autowired
    private IStockRouteService stockRouteService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,@CurrentUser SysUserEntity user) {
    	params.put("companyId", user.getCompanyId());
    	params.put("branchId", user.getBranchId());
        //查询列表数据
        Query query = new Query(params);

        List<StockRouteModel> stockRouteList = stockRouteService.queryList(query);
        int total = stockRouteService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(stockRouteList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id) {
    	StockRouteModel stockRoute = stockRouteService.queryObject(id);
        return R.ok().put("stockRoute", stockRoute);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody StockRouteModel stockRoute,@CurrentUser SysUserEntity user) {
    	stockRoute.setCompanyId(user.getCompanyId());
    	stockRoute.setBranchId(user.getBranchId());
    	stockRoute.setPersonId(user.getPersonId());
    	stockRoute.setRouteDate(new Date());
    	stockRoute.setStatus(1);
    	stockRoute.setCreateUser(user.getId());
    	stockRoute.setCreateTime(new Date());
        int result=stockRouteService.save(stockRoute);
		if(result>0){
        	return R.ok();
        }else{
        	return R.failure();
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody StockRouteModel stockRoute,@CurrentUser SysUserEntity user) {
    	stockRoute.setCompanyId(user.getCompanyId());
    	stockRoute.setBranchId(user.getBranchId());
    	stockRoute.setPersonId(user.getPersonId());
    	stockRoute.setPersonId(user.getPersonId());
    	stockRoute.setUpdateUser(user.getId());
    	stockRoute.setUpdateTime(new Date());
        int result=stockRouteService.update(stockRoute);
		if(result>0){
        	return R.ok();
        }else{
        	return R.failure();
        }
    }
    
    @RequestMapping("/import")
    public R importData(@RequestParam MultipartFile file,@CurrentUser SysUserEntity user) {
    	P result=P.ok();
    	try {
			Map<String, Object> baseMap=new HashMap<String, Object>();
			baseMap.put("company_id", user.getCompanyId());
			baseMap.put("branch_id", user.getBranchId());
			baseMap.put("person_id", user.getPersonId());
			baseMap.put("create_user", user.getId());
			baseMap.put("create_time", new Date());
			boolean xlsxFormat=false;
			if(file.getOriginalFilename().endsWith("xlsx")||file.getOriginalFilename().endsWith("XLSX")){
				xlsxFormat=true;
			}
			result=stockRouteService.importData(baseMap,"stock_route",file.getInputStream(),xlsxFormat);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
    	if(result.isR()){
    		return R.ok();
    	}else{
    		return R.failure();
    	}
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] ids,@CurrentUser SysUserEntity user) {
    	Map<String, Object> params=new HashMap<String, Object>();
    	params.put("ids", ids);
    	params.put("updateUser", user.getId());
    	params.put("updateTime", new Date());
        int result=stockRouteService.deleteLogicByMap(params);
		if(result>0){
        	return R.ok();
        }else{
        	return R.failure();
        }
    }

    /**
     * 查询在途车辆信息
     * @param ids
     * @return
     */
    @RequestMapping("/selectStockRouteList")
    public R selectStockRouteList(@RequestBody String[] ids){
        List<StockRouteModel> list = stockRouteService.selectStockRouteList(ids);
        return R.ok().put("data",list);
    }

}
